<template>
  <div class="app-container">
    <el-form ref="elFormSetting" :model="formUpwardsData" :rules="rules" size="medium" label-width="120px">
      <view>目前数据库尚未启用，向上级连设置使用config里的配置文件。</view>
      <!--级联设置-->
      <el-form-item label="上级国标地址" prop="upper_host">
        <el-input
          v-model="formUpwardsData.upper_host"
          placeholder="请输入上级国标服务器地址"
          :maxlength="256"
          show-word-limit
          clearable
          prefix-icon="el-icon-mobile"
          :style="{width: '100%'}"
        />
      </el-form-item>
      <el-form-item label="上级国标端口" prop="upper_port">
        <el-input
          v-model="formUpwardsData.upper_port"
          placeholder="请输入上级国标服务器端口"
          clearable
          :style="{width: '100%'}"
        />
      </el-form-item>
      <el-form-item label="上级国标域" prop="upper_realm">
        <el-input
          v-model="formUpwardsData.upper_realm"
          placeholder="请输入上级国标服务器域"
          clearable
          :style="{width: '100%'}"
        />
      </el-form-item>
      <el-form-item label="上级账号" prop="upper_account">
        <el-input
          v-model="formUpwardsData.upper_account"
          placeholder="请输入上级国标账号"
          :maxlength="64"
          show-word-limit
          clearable
          prefix-icon="el-icon-user"
          :style="{width: '100%'}"
        />
      </el-form-item>
      <el-form-item label="验证密码" prop="upper_password">
        <el-input
          v-model="formUpwardsData.upper_password"
          placeholder="请输入上级国标验证密码"
          :maxlength="256"
          show-word-limit
          clearable
          prefix-icon="el-icon-mobile"
          :style="{width: '100%'}"
        />
      </el-form-item>
      <el-form-item label="心跳时间" prop="keep_alive_second">
        <el-input
          v-model="formUpwardsData.keep_alive_second"
          placeholder="请输入上级国标心跳时间"
          :maxlength="256"
          show-word-limit
          clearable
          prefix-icon="el-icon-heart"
          :style="{width: '100%'}"
        />
      </el-form-item>
      <el-form-item label="本级账号">
        <el-input
          v-model="formUpwardsData.self_account"
          placeholder="请输入本级账号"
          :maxlength="64"
          show-word-limit
          clearable
          prefix-icon="el-icon-user"
          :style="{width: '100%'}"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="in_use">
<!--        使用toggle,1是启用，0是停用 -->
        <el-switch
          v-model="formUpwardsData.in_use"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <!--级联代码结束-->
      <el-form-item size="large">
        <el-button type="primary" :loading="loading" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { load, save } from '@/api/upwards'
export default {
  name: 'Setting',
  components: {},
  props: [],
  data() {
    return {
      formUpwardsData: {
        upper_host: undefined,
        upper_port: undefined,
        upper_realm: undefined,
        upper_password: undefined,
        keep_alive_second: 60,
        self_account: undefined,
        in_use: 1
      },
      loading: false,
      rules: {
        upper_host: [{ required: true, message: '请输入上级服务地址', trigger: 'blur' }],
        upper_port: [{ required: true, message: '请输入上级服务端口', trigger: 'blur' }],
        upper_realm: [{ required: true, message: '请输入服务器域', trigger: 'blur' }],
        upper_password: [{ required: true, message: '请输入验证密码', trigger: 'blur' }],
        keep_alive_second: [{ required: true, message: '请输入心跳时间', trigger: 'blur' }],
        self_account: [{ required: true, message: '请输入本级账号', trigger: 'blur' }],
        in_use: [{ required: true, message: '请选择是否启用', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.fetchData()
  },
  mounted() {},
  methods: {
    fetchData() {
      load().then(ret => {
        console.log('ret.data', ret.data)
        this.formUpwardsData = ret.data[0]
      })
    },
    submitForm() {
      this.$refs['elFormSetting'].validate(valid => {
        if (valid) {
          this.loading = true
          save(this.formUpwardsData).then(ret => {
            this.$message({
              message: '保存成功',
              type: 'success'
            })
            this.loading = false
          }).catch(err => {
            console.error(err)
            this.loading = false
          })
        } else {
          this.loading = false
        }
      })
    },
    resetForm() {
      this.$refs['elFormSetting'].resetFields()
    }
  }
}

</script>
<style>
</style>
